package com.start.manager.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.manager.dao.CompanyDao;
import com.start.manager.domain.entity.Company;
import com.start.manager.domain.vo.CompanyVO;
import com.start.manager.domain.vo.OptionsVO;
import com.start.manager.service.ICompanyService;
import com.start.manager.util.MyUtil;
import com.start.manager.util.PageUtil;
import com.start.manager.util.RedisUtil;
import com.start.manager.util.constant.RedisKeyConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lijian
 * @since 2019-12-29
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyDao, Company> implements ICompanyService {

    @Autowired
    private MyUtil myUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public PageUtil findList(CompanyVO companyVO) {
        QueryWrapper<Company> queryWrapper = initQueryWrapper(companyVO);
        if (companyVO.getSize() == 0){
            //获取全部的数据
            List<Company> list = this.baseMapper.selectList(queryWrapper);
            return new PageUtil(list.size(),0,list);
        }
        //分页查询货物详情
        Page<Company> page = new Page<>(companyVO.getPage(),companyVO.getSize());
        queryWrapper.orderByAsc("id");
        Page<Company> pageData = this.baseMapper.selectPage(page, queryWrapper);
        return new PageUtil(pageData.getTotal(),pageData.getCurrent(),pageData.getRecords());

    }

    private QueryWrapper<Company> initQueryWrapper(CompanyVO companyVO) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(companyVO.getName())){
            queryWrapper.like("name",companyVO.getName());
        }
        if (StringUtils.isNotBlank(companyVO.getType())){
            queryWrapper.eq("type",companyVO.getType());
        }
        if (StringUtils.isNotBlank(companyVO.getId())){
            queryWrapper.like("id",companyVO.getId());
        }
        return queryWrapper;
    }

    @Override
    public boolean saveCompany(CompanyVO companyVO) {
        Company company = new Company();
        if (StringUtils.isBlank(companyVO.getId())) {
            company.setId(myUtil.generateId("C"));
        }
        BeanUtils.copyProperties(companyVO,company);
        int insert = this.baseMapper.insert(company);
        return insert > 0 ? true : false;
    }

    @Override
    public List<Company> getOptionsList(OptionsVO optionsVO) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",optionsVO.getType());
        queryWrapper.orderByAsc("id");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public String getRedis(String companyId) {
        if (StringUtils.isBlank(companyId)){
            return "未知";
        }
        String value = (String) redisUtil.hget(RedisKeyConstant.COMPANY, companyId);
        if (StringUtils.isNotBlank(value)) {
            Company company = JSONObject.parseObject(value).toJavaObject(Company.class);
            return company.getName();
        }
        Company company = this.baseMapper.selectById(companyId);
        if (Objects.isNull(company)){
            return "未知";
        }
        redisUtil.hset(RedisKeyConstant.COMPANY,companyId, JSONObject.toJSONString(company));
        return company.getName();

    }
}
